package com.sunday.volunteer.admin.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;

import com.sunday.common.event.EventBus;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.ClearEditText;
import com.sunday.member.base.BaseActivity;
import com.sunday.member.event.ExitApp;
import com.sunday.volunteer.admin.BaseApp;
import com.sunday.volunteer.admin.R;
import com.sunday.volunteer.admin.event.LoginEvent;
import com.sunday.volunteer.admin.http.VolunteerClient;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
/*
* 重置密码
* */

public class RestMemberPwdActivity extends BaseActivity {

    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.et_oldPwd)
    ClearEditText etOldPwd;
    @Bind(R.id.et_newPwd)
    ClearEditText etNewPwd;
    @Bind(R.id.et_confirmPwd)
    ClearEditText etConfirmPwd;

    private String phone, code,oldPwd, newPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_restmember_pwd);
        ButterKnife.bind(this);
        titleView.setText("重置密码");
    }

    //restPwd
    private void restPwd() {
        showLoadingDialog(0);
        Call<ResultDO> call = VolunteerClient.getVolunteerAdapter().updatePassword(BaseApp.getInstance().getMember().getId(), oldPwd,newPwd,code);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                dissMissDialog();
                if (response.body() == null || isFinish) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    ToastUtils.showToast(mContext, "重置成功，请重新登陆");
                    EventBus.getDefault().post(new ExitApp());
                    Intent intent = new Intent(mContext,LoginActivity.class);
                    startActivity(intent);
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, "网络错误");
            }
        });
    }

    @OnClick(R.id.btn_submit)
    public void onClick() {
        newPwd = etNewPwd.getText().toString().trim();
        oldPwd = etOldPwd.getText().toString().trim();
        String new2 = etConfirmPwd.getText().toString().trim();
        if (TextUtils.isEmpty(newPwd)){
            ToastUtils.showToast(mContext,"请输入新密码");
            return;
        }else if (TextUtils.isEmpty(new2)){
            ToastUtils.showToast(mContext,"请确认新密码");
            return;
        }else if (TextUtils.isEmpty(oldPwd)){
            ToastUtils.showToast(mContext,"请确认原始密码");
            return;
        }
        if (!newPwd.equals(new2)){
            ToastUtils.showToast(mContext,"两次密码输入不匹配");
            return;
        }
        phone = getIntent().getStringExtra("phone");
        code = getIntent().getStringExtra("code");
        restPwd();
    }
}
